package com.aspose.cells;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

class zcel {
   private Workbook a = null;
   private com.aspose.cells.b.a.d.zm b = null;
   private com.aspose.cells.a.f.zj c = null;
   private zok d = null;
   private boolean e = true;
   private com.aspose.cells.a.f.zi f = null;

   zcel(Workbook var1, boolean var2) {
      this.a = var1;
      this.e = var2;
   }

   void a(com.aspose.cells.b.a.d.zm var1) throws Exception {
      this.b = var1;

      try {
         this.a();
      } finally {
         if (this.a.b != null && this.a.b.b != null) {
            this.a.b.b.a();
         }

      }

   }

   private void a() throws Exception {
      this.q();
      this.d();
      this.o();
      this.n();
      this.m();
      this.l();
      this.g();
      this.a(this.c);
      String var1 = "xl/workbook.bin";
      zcei var2 = new zcei(this, this.d);
      var2.a(var1, this.c);
      String var3 = "xl/styles.bin";
      zcdq var4 = new zcdq(this, this.d);
      var4.a(var3, this.c);
      if ((this.a.getWorksheets().g.c() & 4294967295L) != 0L) {
         String var5 = "xl/sharedStrings.bin";
         zcdj var6 = new zcdj(this, this.a);
         var6.a(var5, this.c);
      }

      this.k();
      this.j();
      this.i();
      this.h();
      this.c();
      this.b();
      this.p();
      this.c.f_();
   }

   private void b() throws Exception {
      zazw var1 = this.a.getWorksheets().k;
      if (var1 != null && var1.getCount() != 0) {
         var1.a(true);
         zcdf var2 = new zcdf(this.d, this, this.c, var1);
         var2.a();
         if (this.d.D.b != null) {
            String var3 = "xl/revisions/_rels/revisionHeaders.bin.rels";
            zcfx var4 = zasr.a(var3, this.c, true);
            ztt.a(var4, this.d.D.b);
            var4.e();
         }

         var2.b();

         for(int var6 = 0; var6 < var1.getCount(); ++var6) {
            zazv var7 = var1.a(var6);
            String var5 = "xl/revisions/revisionLog" + zase.y(var6 + 1) + ".bin";
            var2.a(var5, var7);
         }

      }
   }

   private boolean a(String var1, boolean var2) throws Exception {
      boolean var3 = false;
      if (!var2) {
         this.b(var1, "xl/vbaProject.bin");
         var3 = true;
      }

      this.b("xl/_rels/" + com.aspose.cells.b.a.d.zk.b(var1) + ".rels", "xl/_rels/vbaProject.bin.rels");
      znt var4 = this.a.b.c("application/vnd.ms-office.vbaProjectSignature");
      if (var4 != null) {
         String var5 = var4.c;
         if (var5.charAt(0) == '/') {
            var5 = var5.substring(1);
         }

         this.b(var5, var5);
      }

      return var3;
   }

   private void c() throws Exception {
      if (this.a.hasMacro()) {
         VbaProject var1 = this.a.getWorksheets().l;
         boolean var2 = false;
         if (var1.p != null) {
            var2 = this.a(var1.p, var1.q);
         }

         if (!var2) {
            com.aspose.cells.b.a.d.zm var3 = var1.a((zvh)null);
            if (var3 != null) {
               int var4 = (int)var3.h();
               byte[] var5 = new byte[var4];
               var3.a(var5, 0, var4);
               var3.a();
               String var6 = "xl/vbaProject.bin";
               com.aspose.cells.a.f.zh var7 = this.c.a(var6);
               var7.a(DateTime.getNow());
               this.c.a(var7);
               this.c.b(var5, 0, var5.length);
               this.c.b();
            }
         }

      }
   }

   private void d() throws Exception {
      if (this.a.b != null) {
         this.a("xl/diagrams/");
         this.a("xl/queryTables");
         this.a("userCustomization/");
         if (this.a.isDigitallySigned()) {
            this.a("_xmlsignatures/");
         }

         this.a("customXml/");
         this.a("xl/slicerCaches/");
         this.a("xl/slicers/");
         this.b("xl/connections.bin", "xl/connections.bin");
         this.b("xl/attachedToolbars.bin", "xl/attachedToolbars.bin");
         this.a("vstoDataStore/");
         this.e();
         this.f();
      }
   }

   private void e() throws Exception {
      String var1 = "xl/embeddings";
      Iterator var2 = this.f.iterator();

      while(var2.hasNext()) {
         com.aspose.cells.a.f.zh var3 = (com.aspose.cells.a.f.zh)var2.next();
         if (var3.b().startsWith(var1) && !this.a.b.b(com.aspose.cells.b.a.d.zk.b(var3.b()))) {
            this.a(var3, var3.b());
         }
      }

   }

   private void f() throws Exception {
      if (this.a.b != null && this.a.b.k.size() != 0) {
         ArrayList var1 = this.a.b.k;

         for(int var2 = 0; var2 < var1.size(); ++var2) {
            zpk var3 = (zpk)var1.get(var2);
            if (!var3.c.startsWith("customXml")) {
               this.b(var3.b, var3.c);
               String var4 = zase.ak(var3.b);
               String var5 = zase.ak(var3.c);
               this.a(var4, var5);
            }
         }

      }
   }

   private void a(String var1, String var2) throws Exception {
      if (this.f.a(var1, true) != -1) {
         zcfw var3 = zasr.a(this.f, var1, true);
         ArrayList var4 = zajh.a(var3);
         var3.c();
         this.a.b.a((Collection)var4);
         zcfx var5 = a(var2, this.c);
         ztt.a(var5, var4);
         var5.e();
      }

   }

   private void a(com.aspose.cells.a.f.zj var1) throws Exception {
      zbqf var2 = this.a.getWorksheets().v();
      if (this.d.t.size() != 0) {
         Iterator var3 = this.d.t.keySet().iterator();

         while(var3.hasNext()) {
            int var4 = (Integer)var3.next();
            zqe var5 = (zqe)this.d.t.get(var4);
            zbqg var6 = var2.a(var4);
            String var7 = var6.k();
            String var8 = "xl/" + var5.e;
            String var9 = "xl/externalLinks/_rels/" + com.aspose.cells.a.c.zt.b(var8) + ".rels";
            if (var6.a() == 0 || var6.a() == 4) {
               String var10 = var6.l();
               String var11 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLinkPath";
               if (var6.a() == 4) {
                  String var12 = null;
                  String var13 = null;
                  String[] var14 = new String[]{var12};
                  String[] var15 = new String[]{var13};
                  var6.a(var14, var15);
                  var12 = var14[0];
                  var13 = var15[0];
                  var10 = var13;
                  var11 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject";
               }

               if (var10 != null && !"".equals(var10)) {
                  var10 = com.aspose.cells.b.a.zv.a(var10, " ", "%20");
                  this.a(var9, var11, var10, "External");
               }
            }

            zcbv var16 = new zcbv(this, var6);
            var16.a(var8, var1);
         }

      }
   }

   private void g() throws Exception {
      zto var1 = new zto(this.d);
      var1.a(this.c);
   }

   private void a(zol var1) throws Exception {
      if (var1.n.a != null) {
         String var2 = "xl/drawings/" + var1.n.b;
         zub var3 = new zub(var1);
         var3.a(this.c, var2);
         String var4 = "xl/drawings/_rels/";
         String var5 = var4 + var1.n.b + ".rels";
         if (var1.n.c.size() > 0) {
            zcfx var6 = a(var5, this.c);
            ztt.a(var6, var1.n.c);
            var6.e();
         }

         int var9 = var1.n.g.size();
         if (var9 != 0) {
            for(int var7 = 0; var7 < var9; ++var7) {
               zom var8 = (zom)var1.n.g.get(var7);
               this.a(var8);
               this.d.a(var8.b);
            }

         }
      }
   }

   private void a(zom var1) throws Exception {
      String var2 = "xl/media/" + var1.b;
      com.aspose.cells.a.f.zh var3 = this.c.a(var2);
      var3.a(DateTime.getNow());
      this.c.a(var3);
      this.c.b(var1.a, 0, var1.a.length);
      this.c.b();
   }

   private void h() throws Exception {
      for(int var1 = 0; var1 < this.d.c.size(); ++var1) {
         zol var2 = (zol)this.d.c.get(var1);
         this.b(var2);
      }

   }

   private void b(zol var1) throws Exception {
      if (var1.a.getType() == 1) {
         this.l(var1);
      } else if (var1.a.getType() == 2) {
         this.k(var1);
      }

      this.j(var1);
      this.i(var1);
      this.h(var1);
      this.a(var1);
      this.g(var1);
      this.f(var1);
      zavb.a(var1.a, false);
      this.d(var1);
      this.c(var1);
      this.e(var1);
      this.m(var1);
      zud.a(this.c, var1);
   }

   private void c(zol var1) throws Exception {
      int var2 = var1.o.size();
      if (var2 != 0) {
         for(int var3 = 0; var3 < var2; ++var3) {
            zob var4 = (zob)var1.o.get(var3);
            if (!var4.a.isLink()) {
               byte[] var5 = var4.a.h();
               String var6 = "xl/embeddings/" + var4.e;
               com.aspose.cells.a.f.zh var7 = this.c.a(var6);
               var7.a(DateTime.getNow());
               this.c.a(var7);
               this.c.b(var5, 0, var5.length);
               this.c.b();
            }
         }

      }
   }

   private void a(zqc var1) throws Exception {
      String var2 = com.aspose.cells.b.a.d.zk.b(var1.d);
      String var3 = "xl/pivotCache/" + var2;
      this.a(var3, var3, false);
      String var4 = "xl/pivotCache/_rels/" + var2 + ".rels";
      this.a(var4, var4, false);
      if (this.f != null) {
         if (this.f.a(var4, true) != -1) {
            zcfw var5 = zasr.a(this.f, var4, true);
            HashMap var6 = zajh.b(var5);
            var5.c();
            if (var6 != null && var6.size() > 0) {
               Iterator var7 = var6.values().iterator();

               while(var7.hasNext()) {
                  zqe var8 = (zqe)var7.next();
                  if (var8 != null) {
                     String var9 = var8.e;
                     var9 = "xl/pivotCache/" + var9;
                     this.a(var9, var9, false);
                  }
               }
            }
         }

      }
   }

   private void i() throws Exception {
      ArrayList var1 = this.d.h;
      if (var1 != null) {
         Iterator var2 = var1.iterator();

         while(true) {
            while(var2.hasNext()) {
               zqc var3 = (zqc)var2.next();
               zauk var4 = var3.a;
               if (var4.E != null) {
                  this.a(var3);
               } else {
                  String var5;
                  String var6;
                  String var7;
                  if (var3.f != null) {
                     String var8 = "";
                     int var9 = var3.f.indexOf(".");
                     if (var9 != -1) {
                        var8 = var3.f.substring(0, 0 + var9);
                        var5 = "xl/pivotCache/" + var8 + ".bin";
                        var6 = "xl/pivotCache/_rels/" + var8 + ".bin.rels";
                        var7 = "xl/pivotCache/pivotCacheRecords" + var8.substring(20) + ".bin";
                     } else {
                        var5 = "xl/pivotCache/" + var3.f;
                        var6 = "xl/pivotCache/_rels/" + var3.f + ".rels";
                        var7 = "xl/pivotCache/pivotCacheRecords" + var3.f.substring(20);
                     }
                  } else {
                     var5 = "xl/pivotCache/pivotCacheDefinition" + var3.g + ".bin";
                     var6 = "xl/pivotCache/_rels/pivotCacheDefinition" + var3.g + ".bin.rels";
                     var7 = "xl/pivotCache/pivotCacheRecords" + var3.g + ".bin";
                  }

                  zccs var15 = new zccs(this, var4);
                  var15.a(var5, this.c);
                  if (var4.C != null && var4.C.size() > 0) {
                     ArrayList var17 = new ArrayList();

                     String var11;
                     for(Iterator var10 = var4.C.keySet().iterator(); var10.hasNext(); com.aspose.cells.b.a.a.ze.a(var17, var4.C.get(var11))) {
                        var11 = (String)var10.next();
                        zqe var12 = (zqe)var4.C.get(var11);
                        int var13 = var12.e.indexOf(".");
                        if (var13 != -1) {
                           String var14 = var12.e.substring(0, 0 + var13) + this.d.p;
                           var12.e = var14;
                        }
                     }

                     int var19 = var5.lastIndexOf("/");
                     var5 = "xl/pivotCache/_rels/" + var5.substring(var19 + 1) + ".rels";
                     zcfx var20 = zasr.a(var5, this.c, true);
                     ztt.a(var20, var17);
                     var20.e();
                  } else if (var4.n != null && var4.n.a != null) {
                     String var16 = "pivotCacheRecords" + var3.g + ".bin";
                     this.a(var6, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheRecords", var16, (String)null);
                  }

                  if (var4.n != null && var4.n.a != null) {
                     zccu var18 = new zccu(this, var4);
                     var18.a(var7, this.c);
                  }
               }
            }

            return;
         }
      }
   }

   private void d(zol var1) throws Exception {
      PivotTableCollection var2 = var1.a.b;
      if (var2 != null && var2.getCount() != 0) {
         for(int var3 = 0; var3 < var2.getCount(); ++var3) {
            PivotTable var4 = var2.get(var3);
            int var5 = (Integer)this.d.i.get(var4);
            String var6 = "xl/pivotTables/pivotTable" + var5 + ".bin";
            String var7 = "xl/pivotTables/_rels/pivotTable" + var5 + ".bin.rels";
            if (var4.d == null) {
               return;
            }

            zqc var8 = this.d.a(var4.d);
            zccw var9 = new zccw(this, var4, var8.c);
            var9.a(var6, this.c);
            String var10 = "../pivotCache/pivotCacheDefinition" + var8.g + ".bin";
            this.a(var7, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition", var10, (String)null);
         }

      }
   }

   private void e(zol var1) throws Exception {
      if (var1.v.size() != 0) {
         com.aspose.cells.b.a.a.zc var2 = com.aspose.cells.b.a.a.zf.a(var1.v);

         while(var2.c()) {
            ListObject var3 = (ListObject)var2.a();
            String var4 = (String)var2.b();
            zqe var5 = zajh.a(var1.h, var4);
            String var6 = "xl" + var5.e.substring(2);
            zcec var7 = new zcec(this, var3);
            var7.a(var6, this.c);
         }

      }
   }

   private void f(zol var1) throws Exception {
      zog var2 = var1.l;
      if (var2 != null && var2.c != null && var2.c.size() != 0) {
         String var3 = "xl/drawings/" + var2.b;
         zcfx var4 = a(var3, this.c);
         ztl var5 = new ztl(var1);
         var5.a(var4);
         var4.e();
         if (var2.c != null && var2.c.size() > 0) {
            String var6 = "xl/drawings/_rels/" + var2.b + ".rels";
            var4 = a(var6, this.c);
            ztt.a(var4, var2.c);
            var4.e();
         }

      }
   }

   private void g(zol var1) throws Exception {
      for(int var2 = 0; var2 < var1.m.f.size(); ++var2) {
         znw var3 = (znw)var1.m.f.get(var2);
         String var4 = "xl/charts/chart" + zase.y(var3.b) + ".xml";
         zcfx var5 = a(var4, this.c);
         zsz var6 = new zsz(var3);
         var6.a(var5);
         var5.e();
         this.a(var3);
         if (var3.d.size() != 0) {
            this.b(var3);
         }

         if (var3.f != null) {
            this.c(var3);
         }
      }

   }

   private void a(znw var1) throws Exception {
      int var2 = var1.c.size();
      if (var2 != 0) {
         for(int var3 = 0; var3 < var2; ++var3) {
            zom var4 = (zom)var1.c.get(var3);
            this.a(var4);
            this.d.a(var4.b);
         }

      }
   }

   private void b(znw var1) throws Exception {
      int var2 = var1.b;
      String var3 = "xl/charts/_rels/chart" + zase.y(var2) + ".xml.rels";
      zcfx var4 = a(var3, this.c);
      ztt.a(var4, var1.d);
      var4.e();
   }

   private void c(znw var1) throws Exception {
      String var2 = "xl/drawings/" + var1.f.b;
      zcfx var3 = a(var2, this.c);
      zti var4 = new zti(var1.f);
      var4.a(var3);
      var3.e();
      ArrayList var5 = var1.f.c;
      if (var5 != null && var5.size() > 0) {
         String var6 = "xl/drawings/_rels/" + var1.f.b + ".rels";
         var3 = a(var6, this.c);
         ztt.a(var3, var5);
         var3.e();
      }

      this.d.a("/" + var2, "application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml");
   }

   private void h(zol var1) throws Exception {
      if (var1.m.a != null) {
         String var2 = "xl/drawings/" + var1.m.b;
         String var3 = "xl/drawings/_rels/" + var1.m.b + ".rels";
         zcfx var4 = a(var2, this.c);
         zti var5 = new zti(var1.m);
         var5.a(var4);
         var4.e();
         if (var1.m.c != null && var1.m.c.size() > 0) {
            var4 = a(var3, this.c);
            ztt.a(var4, var1.m.c);
            var4.e();
         }

      }
   }

   private void i(zol var1) throws Exception {
      if (var1.c()) {
         String var2 = "xl/" + var1.g;
         zcbg var3 = new zcbg(this, var1);
         var3.a(var2, this.c);
      }
   }

   private void j(zol var1) throws Exception {
      if (var1.h != null && var1.h.size() != 0) {
         String var2 = "xl/worksheets/_rels/sheet" + var1.d + ".bin.rels";
         if (var1.a.getType() == 2) {
            var2 = "xl/chartsheets/_rels/sheet" + var1.d + ".bin.rels";
         }

         zcfx var3 = a(var2, this.c);
         ztt.a(var3, var1.h);
         var3.e();
      }

   }

   private void k(zol var1) throws Exception {
      String var2 = "xl/chartsheets/sheet" + var1.d + ".bin";
      zcaz var3 = new zcaz(this, this.d);
      var3.a(var1, var2, this.c);
   }

   private void l(zol var1) throws Exception {
      String var2 = "xl/worksheets/sheet" + var1.d + ".bin";
      zcek var3 = new zcek(this, this.d);
      var3.a(var1, var2, this.c);
   }

   private void j() throws Exception {
      String var1 = "xl/_rels/workbook.bin.rels";
      zcfx var2 = a(var1, this.c);
      ztt.a(var2, this.d.e);
      var2.e();
   }

   private void k() throws Exception {
      String var1 = "xl/theme/theme1.xml";
      zcfx var2 = a(var1, this.c);
      zua var3 = new zua(this.d);
      var3.a(var2);
      var2.e();
      if (this.a.b != null) {
         String var4 = "xl/theme/_rels/" + com.aspose.cells.b.a.d.zk.b(this.a.b.f) + ".rels";
         String var5 = "xl/theme/_rels/theme1.xml.rels";
         this.b(var4, var5);
      }

   }

   private void l() throws Exception {
      if (this.d.h()) {
         String var1 = "docProps/custom.xml";
         zcfx var2 = a(var1, this.c);
         zth var3 = new zth(this.d);
         var3.a(var2);
         var2.e();
      }
   }

   private void m() throws Exception {
      String var1 = "docProps/app.xml";
      zcfx var2 = a(var1, this.c);
      ztf var3 = new ztf(this.d);
      var3.a(var2);
      var2.e();
   }

   private void n() throws Exception {
      String var1 = "docProps/core.xml";
      zcfx var2 = a(var1, this.c);
      ztg var3 = new ztg(this.d);
      var3.a(var2);
      var2.e();
   }

   private void o() throws Exception {
      String var1 = "_rels/.rels";
      zcfx var2 = a(var1, this.c);
      ztt.b(this.d, var2);
      var2.e();
   }

   private void p() throws Exception {
      zcfx var1 = a("[Content_Types].xml", this.c);
      ztd var2 = new ztd(this.d);
      var2.a(var1);
      var1.e();
   }

   private void a(String var1) throws Exception {
      this.a(var1, var1, true);
   }

   private void b(String var1, String var2) throws Exception {
      this.a(var1, var2, false);
   }

   private void a(String var1, String var2, boolean var3) throws Exception {
      Iterator var4 = this.f.iterator();

      while(true) {
         com.aspose.cells.a.f.zh var5;
         do {
            do {
               if (!var4.hasNext()) {
                  return;
               }

               var5 = (com.aspose.cells.a.f.zh)var4.next();
            } while(!var3 && !com.aspose.cells.b.a.zv.b(var5.b(), var1));
         } while(var3 && !var5.b().startsWith(var1));

         if (var3) {
            var2 = var5.b();
         }

         com.aspose.cells.a.f.zh var6 = this.c.a(var2);
         var6.a(DateTime.getNow());
         this.c.a(var6);
         byte[] var7 = new byte[1024];
         boolean var8 = false;
         com.aspose.cells.b.a.d.zm var9 = this.f.a(var5);

         int var10;
         do {
            var10 = var9.a(var7, 0, var7.length);
            this.c.b(var7, 0, var10);
         } while(var10 > 0);

         var9.a();
         this.c.b();
      }
   }

   private void a(com.aspose.cells.a.f.zh var1, String var2) throws Exception {
      com.aspose.cells.a.f.zh var3 = this.c.a(var2);
      var3.a(DateTime.getNow());
      this.c.a(var3);
      byte[] var4 = new byte[1024];
      boolean var5 = false;
      com.aspose.cells.b.a.d.zm var6 = this.f.a(var1);

      int var7;
      do {
         var7 = var6.a(var4, 0, var4.length);
         this.c.b(var4, 0, var7);
      } while(var7 > 0);

      var6.a();
      this.c.b();
   }

   private void q() throws Exception {
      this.a.getWorksheets().o();
      if (this.a.b != null) {
         this.f = this.a.b.b.b;
      }

      this.d = new zok(this.a, this.f, this.e, false);
      if (this.d.c.size() == 0) {
         throw new CellsException(6, "Dot not support export a wbook without normal worksheet to excel 2007 format");
      } else {
         this.c = new com.aspose.cells.a.f.zj(this.b);
         this.c.b(9);
      }
   }

   private static zcfx a(String var0, com.aspose.cells.a.f.zj var1) throws Exception {
      com.aspose.cells.a.f.zh var2 = var1.a(var0);
      var2.a(DateTime.getNow());
      var1.a(var2);
      zcfx var3 = new zcfx(var1, Encoding.getUTF8());
      var3.c(true);
      var3.a((int)1);
      return var3;
   }

   void a(String var1, String var2, String var3, String var4) throws Exception {
      ArrayList var5 = new ArrayList();
      zqe var6 = new zqe("rId1", var2, var3, var4);
      com.aspose.cells.b.a.a.ze.a(var5, (Object)var6);
      zcfx var7 = a(var1, this.c);
      ztt.a(var7, var5);
      var7.e();
   }

   static void a(String var0, com.aspose.cells.b.a.d.zh var1, com.aspose.cells.a.f.zj var2) throws Exception {
      com.aspose.cells.a.f.zh var3 = var2.a(var0);
      var3.a(DateTime.getNow());
      var2.a(var3);
      var1.b(0L);
      byte[] var4 = new byte[1024];
      boolean var5 = false;

      int var6;
      do {
         var6 = var1.a(var4, 0, var4.length);
         var2.b(var4, 0, var6);
      } while(var6 > 0);

   }

   private void m(zol var1) throws Exception {
      if (var1.t != null && var1.t.size() != 0) {
         Iterator var2 = var1.t.keySet().iterator();

         while(var2.hasNext()) {
            String var3 = (String)var2.next();
            com.aspose.cells.a.f.zh var4 = this.c.a(var3);
            var4.a(DateTime.getNow());
            this.c.a(var4);
            byte[] var5 = (byte[])((byte[])var1.t.get(var3));
            this.c.b(var5, 0, var5.length);
            this.c.b();
         }

      }
   }
}
